var ctx=window.daijia.ctx;

var offset=0;
var limit=20;
var pagingLimit=20;//显示分页时的条数
var currentTotalElements=0;//当前显示总条数


var pageLoading=true;//是否能滚动加载
var hasShowPages=false;//是否已显示分页
var firstLoadPages=true;//分页初始化时 不自动加载
var thisButton=null;

$(document).ready(function(){
	
	
	initdata(false);
	
	$(document).on("keydown","body",function(){
		if(window.event.keyCode == 13){
			$("#searchButton").click();
		}
	});
	
	
	$(document).on("click",".deletethis",function(){
		thisButton = $(this);
		var confirm = $.scojs_confirm({
			content: "<font style='color:red;font-size:20px;'>您确认要删除该等级吗?删除等级后关联的客户和企业等级将设置为空!</font>",
			action:deleteGrade
		});
		confirm.show();
	});
	
	function deleteGrade() {
		$.post(ctx+'/member/deleteGrade/'+thisButton.attr("data-value"),function(data){
			if(data.success){
				$.scojs_message(data.message, $.scojs_message.TYPE_OK);
				setTimeout(function (){
					window.location.reload(true);
				}, 1000);
			} else {
				$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
			}
		},'json');
	}
	$(document).on("click",".disposeGrade",function(){
		thisButton = $(this);
		var confirm = $.scojs_confirm({
			content: "<font style='color:red;font-size:20px;'>您确认要处理等级吗?处理后该公司下等级为空的客户和企业将设置为该等级!</font>",
			action:disposeGrade
		});
		confirm.show();
	});
	function disposeGrade() {
		$.post(ctx+'/member/disposeGrade/'+thisButton.attr("data-value"),function(data){
			if(data.success){
				$.scojs_message(data.message, $.scojs_message.TYPE_OK);
				setTimeout(function (){
					window.location.reload(true);
				}, 1000);
			} else {
				$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
			}
		},'json');
	}
});


function initdata(isappend){
	
	$.ajax({
        url : ctx+'/member/gradelistData',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"pager.offset":offset,
        	"limit":limit,
        },
        success : function(result){
        	var newcontent = '';
        	$("#NoData").text("");
        	
    		$(result).each(function(index,data){
    			var mainButton="";
    			var selectButton="";
    			mainButton="<a href='"+ctx+"/member/editGrade/"+data.id+"?name="+data.name+"' class='btn btn-primary btn-xs'>编辑</a>";
    			selectButton+="<li><a href='javascript:;' data-value="+data.id+"  class='btn-link deletethis'>删除</a></li>";
    			if (data.companyId != null && data.companyId != 0) {
    				selectButton+="<li><a href='javascript:;' data-value="+data.id+" class='btn-link disposeGrade'>处理</a></li>";
    			}
    			
    			var allButton="<div class='btn-group'>"+
					mainButton+
				  "<button type='button' class='btn "+"btn-primary"+" btn-xs dropdown-toggle' data-toggle='dropdown' aria-expanded='false'>"+
				    "<span class='caret'></span>"+
				    "<span class='sr-only'>Toggle Dropdown</span>"+
				  "</button>"+
				  "<ul class='dropdown-menu' role='menu'>"+
				  	selectButton+
				  "</ul>"+
				"</div>";
    			
    newcontent+="<tr class='grid-data tr-hui' style='display:none;'>"+
					"<td>"+data.name+"</td>"
					if(data.defaultGrade){
						 newcontent+="<td>是</td>";
					}else{
						 newcontent+="<td>否</td>";
					}
    var companyName="-";
    if(data.companyName!=null){
    	companyName=data.companyName;
    }
    newcontent+= "<td>"+companyName+"</td>"+
    			 "<td>"+(new Date(data.created).Format("yyyy-MM-dd"))+"</td>"+
				 "<td>"+allButton+"</td>"+
				"</tr>";
    		});
        		
        		
        	

//        	hideLoading();
        	
        	if(!isappend){
        		$(".table tbody .grid-data").remove();
        	}
        	$(".table tbody").append(newcontent);
        	
        	$(".table tbody .grid-data").each(function(index,element){
        		$(element).fadeIn(200+(index*35));
        	});
        	
        	
        	
        	
        }
	});
	
}

function showLoading(){
	if($('#loadblacklayer').length==0){
		var dw = $(".table").width();
		var dh = $(".table").height();
		var backlayer ="<div id='loadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
		$(".table tbody").append(backlayer);
		$('#loadblacklayer').css({"z-index":"9","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
	}
}

function hideLoading(){
	if($('#loadblacklayer').length>0){
		$('#loadblacklayer').remove();
	}
}
Date.prototype.Format = function(fmt) 
{ 
  var o = { 
    "M+" : this.getMonth()+1,                 //月份 
    "d+" : this.getDate(),                    //日 
    "h+" : this.getHours(),                   //小时 
    "m+" : this.getMinutes(),                 //分 
    "s+" : this.getSeconds(),                 //秒 
    "q+" : Math.floor((this.getMonth()+3)/3), //季度 
    "S"  : this.getMilliseconds()             //毫秒 
  }; 
  if(/(y+)/.test(fmt)) 
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  for(var k in o) 
    if(new RegExp("("+ k +")").test(fmt)) 
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  return fmt; 
}
